/**
 * @Project : algorithm-learning
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/4/16 16:38
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int maxOperations(vector<int> &nums, int k) {
        int res = 0;
        int l = 0;
        int r = nums.size() - 1;

        sort(nums.begin(), nums.end());

        while (l < r) {
            int sum = nums[l] + nums[r];
            if (sum > k) r--;
            else if (sum < k) l++;
            else {
                l++;
                r--;
                res++;
            }
        }

        return res;
    }
};
